//
//  RecordVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/17.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit

class RecordVC: UIViewController {
    enum PickButton: Int {
        case album = 0
        case camera = 1
        case shop = 2
    }
    
    @IBOutlet var pickButtons: [UIButton]!{
        didSet{
            pickButtons.forEach{item in
                item.alpha = 0
            }
        }
    }
    @IBOutlet var pickButtonsBottomConstraints: [NSLayoutConstraint]!{
        didSet{
            pickButtonsBottomConstraints.forEach{item in
                item.constant = -64
            }
        }
    }
    @IBOutlet weak var tipsLabel: UILabel!{
        didSet{
            tipsLabel.alpha = 0
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
       
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    func startAnimation() {
        func animateTipsLabel(){
            UIView.animate(withDuration: 1,
                           delay: 0,
                           options: .curveEaseOut,
                           animations: {
                            self.tipsLabel.alpha = 1
            }, completion: nil)
        }
        
        animate(button: 2, constant: 150) {
            self.animate(button: 1, constant: 30, completion: {
                self.animate(button: 0, constant: 30, completion: {
                   animateTipsLabel()
                })
            })
        }
    }
    
    func animate(button index: Int, constant: CGFloat, completion: (()->Void)? ){
        pickButtonsBottomConstraints[index].constant = constant
        UIView.animate(withDuration: 0.25,
                       delay: 0,
                       usingSpringWithDamping: 0.2,
                       initialSpringVelocity: 0.5,
                       options: .curveEaseOut,
                       animations:
        {
            self.pickButtons[index].alpha = 1
            self.view.layoutIfNeeded()
        }, completion: {
            _ in
            completion?()
        })
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        tipsLabel.alpha = 0
        pickButtonsBottomConstraints.forEach{item in
            item.constant = -64
        }
    }

    @IBAction func buttonClicked(_ sender: UIButton) {
        func toComposingPage(image: UIImage?){
            if let vc: TextComposingVC = .make(from: .main){
                vc.image = image
                vc.modalTransitionStyle = .crossDissolve
                present(vc: vc)
            }
        }
        
        let btn = PickButton(rawValue: pickButtons.index(of: sender)!)!
        switch btn {
        case .album:
            ImagePicker.shared.showWith(presentingViewController: self, sourceType: .savedPhotosAlbum, allowsEditing: true, completion: { image in
                toComposingPage(image: image)
            })
        case .camera:
            ImagePicker.shared.showWith(presentingViewController: self, sourceType: .camera, completion: { image in
                toComposingPage(image: image)
            })
        default: break
        }
    }
    
    @IBAction func unwindRecordVC(_ sender: UIStoryboardSegue) {
    }
}
